﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace MedReg.Classes
{
    [Serializable]
    public class Territory : ISerializable
    {
        private int idField;

        private string nameField;

        private System.Nullable<int> parentField;

        private System.Nullable<int> orderField;

        private string oUZField;

        private System.Nullable<long> kLADRField;

        private static readonly Territory theOneObject = new Territory();

        public static Territory GetTerritory()
        {
            return theOneObject;
        }
        /// <remarks/>
        public int ID
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }

        /// <remarks/>
        public string Name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public System.Nullable<int> Parent
        {
            get
            {
                return this.parentField;
            }
            set
            {
                this.parentField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public System.Nullable<int> Order
        {
            get
            {
                return this.orderField;
            }
            set
            {
                this.orderField = value;
            }
        }

        /// <remarks/>
        public string OUZ
        {
            get
            {
                return this.oUZField;
            }
            set
            {
                this.oUZField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public System.Nullable<long> KLADR
        {
            get
            {
                return this.kLADRField;
            }
            set
            {
                this.kLADRField = value;
            }
        }
        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //throw new NotImplementedException();
            info.AddValue("ID", idField);
            info.AddValue("Name", nameField);
            info.AddValue("Parent", parentField);
            info.AddValue("Order", orderField);
            info.AddValue("OUZ", oUZField);
            info.AddValue("KLADR", kLADRField);
        }
        public Territory()
        {
        }
        public List<Classes.Territory> getTerritoryList(List<Classes.AddressEntity> sAddressEntity)
        {

            var sTerritory = new List<Classes.Territory>(from lv in sAddressEntity select lv.Region);

            var ssSub = sTerritory
                .GroupBy(s => new { s.ID })
                //.GroupBy(s => new { s.ID, s.Name, s.Parent })
                .Select(g => g.First())
                .OrderBy(o => o.ID)
                .ToList();
            return ssSub;
        }
        public List<Classes.Territory> getTerritoryList(Classes.ArrayOfEmployee sEmployee)
        {

            var sTerritory = new List<Classes.Territory>(from lv in sEmployee select lv.Region);

            var ssSub = sTerritory
                .GroupBy(s => new { s.ID })
                //.GroupBy(s => new { s.ID, s.Name, s.Parent })
                .Select(g => g.First())
                .OrderBy(o => o.ID)
                .ToList();
            return ssSub;
        }
    }
}
